import { defineStore, storeToRefs } from 'pinia';
import { messageFn } from '../../utils';
import {
  getContactList,
  getCompanylist,
  postWaterService,
  postWaterStop,
  postWaterRecovery,
  postWaterCancell,
  postOpenWater,
  useWaterNatureChange,
  postMeterChange,
  postOtherChange,
  postPeriodMeterChange,
  getRecordPage,
} from '/@/api/comprehensive/management';
interface managemtype {
  manageuser: '';
  manageCompany: '';
  manageuserList: [];
  manageCompanyList: [];
  recordList: [];

  userInfo:'';
}
export const manageStore = defineStore(' manage_store', {
  state: (): managemtype => {
    return {
      manageuser: '',
      manageCompany: '',
      manageuserList: [],
      manageCompanyList: [],
      recordList: [],
      userInfo:''
    };
  },
  actions: {
    setmanageuser(obj: any) {
      this.manageuser = obj;
    },
    setmanageCompany(obj: any) {
      this.manageCompany = obj;
    },

    // 获得联系人
    async setContactList(obj: object) {
      const { code, data, msg } = await getContactList(obj);
      if (code === 0) {
        this.manageuserList = data;
        return data;
      } else {
        messageFn('error', msg);
      }
    },

    // 获得企业
    async setCompanylist(obj: object) {
      const { code, data, msg } = await getCompanylist(obj);
      if (code === 0) {
        this.manageCompanyList = data;
        return data;
      } else {
        messageFn('error', msg);
      }
    },
    // 过户
    async setWaterService(obj: object) {
      const { code, data, msg } = await postWaterService(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 报停
    async setWaterStop(obj: object) {
      const { code, data, msg } = await postWaterStop(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 恢复
    async setWaterRecovery(obj: object) {
      const { code, data, msg } = await postWaterRecovery(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 销户
    async setWaterCancell(obj: object) {
      const { code, data, msg } = await postWaterCancell(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 开通用水
    async setOpenWater(obj: object) {
      const { code, data, msg } = await postOpenWater(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 用水性质变更
    async useWaterNatureChange(obj: object) {
      const { code, data, msg } = await useWaterNatureChange(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 更换水表
    async setMeterChange(obj: object) {
      const { code, data, msg } = await postMeterChange(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },
    // 周期更换水表
    async setPeriodMeterChange(obj: object) {
      const { code, data, msg } = await postPeriodMeterChange(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },

    // 优惠申请
    async setOtherChange(obj: object) {
      const { code, data, msg } = await postOtherChange(obj);
      if (code === 0) {
        messageFn('success', msg);
        return true;
      } else {
        messageFn('error', msg);
      }
    },

    // 获得账户分页
    async getRecordPageList(obj: object) {
      const { code, data, msg } = await getRecordPage(obj);
      if (code === 0) {
        this.recordList = data.page_result.list;
        return data;
      } else {
        return messageFn('error', msg);
      }
    },

    // 保存报装信息用户
    async setUserInfo(data){
      this.userInfo=data
    },

    setrecordList() {
      this.recordList = [];
    },
  },
});
